/**  
* @Title: DisStructureAnalysisFacadeImpl.java  
* @Package com.dhcc.DRGsHosp.facade.impl  
* @Description: TODO
* @author shaozhiyuan 
* @date 2018年8月31日  
* @version V3.1.0  
*/  
package com.dhcc.DRGsHosp.facade.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.dhcc.DRGsHosp.application.DisStructureAnalysisApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.facade.DisStructureAnalysisFacade;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.dto.DrgsAnalysisDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.DrgsAnalysisAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**  
* @ClassName: DisStructureAnalysisFacadeImpl  
* @Description: 病组结构分析
* @author Administrator  
* @date 2018年8月31日  
* @author shaozhiyuan  
*/
@Named
public class DisStructureAnalysisFacadeImpl implements DisStructureAnalysisFacade{

	@Autowired
	private DisStructureAnalysisApplication disStructureAnalysisApplication;
	
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	
	/**  
	 * @Description: 病组结构分析(表格数据获取) 
	 * @author shaozhiyuan 
	 * @date 2018年8月31日  
	 * @version V3.1.1
	 */  
	@Override
	public List<Map<String, Object>> getStructureData(IndexCal indexCal) {
		String groupScheme=drgsIndexCalFacade.getGroupScheme();	//获取分组方案
		String stdId=drgsIndexCalFacade.getStdID(indexCal.getDateFrom(),indexCal.getDateTo());//获取标杆定位id
		indexCal.setStdId(stdId);
		indexCal.setGroupType(groupScheme);
		List<Map<String, Object>> list=disStructureAnalysisApplication.getStructureData(indexCal);
		return list;
	}

	/**  
	 * @Description:新版病组结构DRGs病组详情
	 * @author shaozhiyuan 
	 * @date 2018年9月5日  
	 * @version V3.1.0  
	 */  
	@Override
	public InvokeResult getDrgsStrucDetail(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo( dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom, dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		String queryType=dto.getQueryType();
		List<Map<String,Object>> result = disStructureAnalysisApplication.getDrgsStrucDetail(DrgsAnalysisAssembler.toEntity(dto));
		List<Map<String,Object>> resultData = new LinkedList<Map<String,Object>>();
		final String flag = queryType;
		
		Collections.sort(result, new Comparator<Map<String,Object>>(){
			public int compare(Map<String,Object> o1,Map<String,Object> o2){
				String s = "";
				if(flag.contains("this")){
					s = "TPCNT";
				}else{
					s="LPCNT";
				}
				
				BigDecimal v1 = (BigDecimal)o1.get(s);
				BigDecimal v2 = (BigDecimal)o2.get(s);
				int value1 = (v1==null ? 0:v1.intValue());
				int value2 = (v2==null ? 0:v2.intValue());
				return value2-value1;
			}
		});
		for(int i=0;i<result.size();i++){
			Map<String,Object> map = result.get(i);
			String key = "";
			if(flag.contains("this")){
				key = "THISORDER";
			}else{
				key="LASTORDER";
			}
			map.put(key, i+1);
			resultData.add(map);
		}
		return InvokeResult.success(resultData);
	}

	/**  
	 * @Description:查看理论内未覆盖DRGs组数情况
	 * @author shaozhiyuan 
	 * @date 2018年9月6日  
	 * @version V3.1.1 
	 */  
	@Override
	public InvokeResult getNotInDrgData(DrgsAnalysisDTO dto) {
		String dateFrom = (DrgsIndex.formateDateFrom(dto.getDateFrom())).substring(0, 7);
		String dateTo = (DrgsIndex.formateDateTo( dto.getDateTo())).substring(0, 7);
		String stdId = drgsIndexCalFacade.getStdID(dateFrom, dateTo);
		dto.setStdId(stdId);
		String verRegion = drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);
		List<Map<String,Object>> result = disStructureAnalysisApplication.getNotInDrgData(DrgsAnalysisAssembler.toEntity(dto));
		return InvokeResult.success(result);
	}

}
